#if UNITY_EDITOR
using Sirenix.OdinInspector;
using Slate;
using UniFramework.Battle;
using UnityEngine;

namespace UniFramework.Skill
{
	public partial class SkillConfig
	{
		public void AddData(object data)
		{
			switch (data)
			{
				case SKillEffectConfigNode effectNode:
					skillEffectList.Add(effectNode);
					break;

				case SkillAudioConfigNode audioNode:
					skillAudioList.Add(audioNode);
					break;

				case SkillAnimationConfigNode animNode:
					animNode.name = animNode.animationClip.name;
					skillAnimationList.Add(animNode);
					break;

				case SkillBuffConfigNode buffNode:
					skillBuffList.Add(buffNode);
					break;

				default:
					throw new System.ArgumentException(
						$"[SkillConfig] 不支持的数据类型：{data?.GetType().Name}");
			}
		}

		[Button("配置技能", ButtonSizes.Large), GUIColor("green")]
		public void LoadSkillData()
		{
			SkillConfig skillConfig = this;
			GameObject go = new GameObject($"~[Skill_{baseSkillConfig.skillId}]");
			Cutscene seq = go.AddComponent<Cutscene>();
			var cutsceneGroup = seq.AddGroup<ActorGroup>();
			cutsceneGroup.actor = go;
			int trackId = 0;
			//特效
			for (int i = 0; i < skillConfig.skillEffectList.Count; i++)
			{
				var data = skillConfig.skillEffectList[i];
				float startTime = LogicFrameConfig.GetRealTime(data.triggerFrame);
				if (trackId == 0)
				{
					cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
					++trackId;
				}
				else
				{
					var data2 = skillConfig.skillEffectList[i - 1];
					float endTime2 = LogicFrameConfig.GetRealTime(data2.endFrame);
					if (startTime <= endTime2)
					{
						cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
						++trackId;
					}
				}

				var track = cutsceneGroup.tracks[trackId - 1] as SkillTrack;
				track.data = skillConfig.baseSkillConfig;
				var clip = track.AddAction<EffectClip>(startTime);
				clip.data = data;
				clip.RefreshTime();
			}

			trackId = 0;
			//音频
			for (int i = 0; i < skillConfig.skillAudioList.Count; i++)
			{
				var data = skillConfig.skillAudioList[i];
				float startTime = LogicFrameConfig.GetRealTime(data.triggerFrame);
				if (trackId == 0)
				{
					cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
					++trackId;
				}
				else
				{
					var data2 = skillConfig.skillAudioList[i - 1];
					float endTime2 = LogicFrameConfig.GetRealTime(data2.endFrame);
					if (startTime <= endTime2)
					{
						cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
						++trackId;
					}
				}

				var track = cutsceneGroup.tracks[trackId - 1] as SkillTrack;
				track.data = skillConfig.baseSkillConfig;
				var clip = track.AddAction<AudioClip>(startTime);
				clip.data = data;
				clip.RefreshTime();
			}

			trackId = 0;
			//动画
			for (int i = 0; i < skillConfig.skillAnimationList.Count; i++)
			{
				var data = skillConfig.skillAnimationList[i];
				float startTime = LogicFrameConfig.GetRealTime(data.triggerFrame);
				if (trackId == 0)
				{
					cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
					++trackId;
				}
				else
				{
					var data2 = skillConfig.skillAnimationList[i - 1];
					float endTime2 = LogicFrameConfig.GetRealTime(data2.endFrame);
					if (startTime <= endTime2)
					{
						cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
						++trackId;
					}
				}

				var track = cutsceneGroup.tracks[trackId - 1] as SkillTrack;
				track.data = skillConfig.baseSkillConfig;
				var clip = track.AddAction<AnimationClip>(startTime);
				clip.data = data;
				clip.RefreshTime();
			}

			trackId = 0;
			//Buff
			for (int i = 0; i < skillConfig.skillBuffList.Count; i++)
			{
				var data = skillConfig.skillBuffList[i];
				float startTime = LogicFrameConfig.GetRealTime(data.triggerFrame);
				if (trackId == 0)
				{
					cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
					++trackId;
				}
				else
				{
					var data2 = skillConfig.skillBuffList[i - 1];
					float endTime2 = LogicFrameConfig.GetRealTime(data2.endFrame);
					if (startTime <= endTime2)
					{
						cutsceneGroup.AddTrack<SkillTrack>(index: trackId);
						++trackId;
					}
				}

				var track = cutsceneGroup.tracks[trackId - 1] as SkillTrack;
				track.data = skillConfig.baseSkillConfig;
				var clip = track.AddAction<BuffClip>(startTime);
				clip.data = data;
				clip.RefreshTime();
			}
		}
	}
}
#endif